package com.example.spring_seatreservation.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.example.spring_seatreservation.Bean.R;
import com.example.spring_seatreservation.entity.Activity;
import com.example.spring_seatreservation.mapper.ActivityMapper;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/activity")
@AllArgsConstructor
public class ActivityController {

    private final ActivityMapper activityMapper;

    @GetMapping("getActivityList")
    public List<Activity> getActivityList(String libId){
        return activityMapper.getActivity(libId == null ? null : Long.parseLong(libId));
    }

    @GetMapping("getActivityTop")
    public List<Activity> getActivityTop(){
        LambdaQueryWrapper<Activity> lambdaQueryWrapper = Wrappers.<Activity>lambdaQuery()
                .orderByDesc(Activity::getTime).last(" limit 5");
        return activityMapper.selectList(lambdaQueryWrapper);
    }

    @PostMapping("submit")
    public Map<String, Object> submit(@RequestBody Activity activity){
        if (activity.getId() == null) {
            activityMapper.insert(activity);
        } else {
            activityMapper.updateById(activity);
        }
        return new R().ok().builder();
    }

    @GetMapping("delete")
    public Map<String, Object> delete(String id){
        activityMapper.deleteById(id);
        return new R().ok().builder();
    }

}
